package org.protester.core;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;

import org.protester.base.EntityBase;

@Entity
public class CommentContainer extends EntityBase {

	@Column(nullable = false)
	// TODO utiliza-lo na condicao para adicionar comentario
	private boolean activeted;

	@Column(nullable = false)
	private int size = 0;

	@OneToMany(cascade = CascadeType.PERSIST, mappedBy = "container")
	private Set<Comment> comments = new HashSet<Comment>();

	public CommentContainer() {
	}

	public boolean isActiveted() {
		return activeted;
	}

	public void setActiveted(boolean on) {
		this.activeted = on;
	}

	public int size() {

		if (size == 0)
			size = comments.size();

		return size;
	}

	public Set<Comment> getComments() {
		return comments;
	}

	public void addComment(Comment comment) throws CommentException {

		if (comment.getParent() != null)
			throw new CommentException(
					"O comentario eh uma replica a um outro, portanto, nao pode ser adicionado ao container");

		if (comment.getContainer() != null)
			throw new CommentException(
					"O comentario ja pertence a um container.");

		this.comments.add(comment);
		comment.setContainer(this);
		this.size++;

	}

}